import {Vector2} from "./Vector2";

export class Box2 {
    min: Vector2 = new Vector2(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
    max: Vector2 = new Vector2(Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY);
    
    constructor(vectors: Vector2[] = []) {
        this.addPoints(vectors);
    }

    addPoints(points: Vector2[]) {
        for (const point of points) {
            this.addPoint(point);
        }
    }
    
    addPoint(point: Vector2) {
        const {x, y} = point;
        this.min.x = Math.min(this.min.x, point.x);
        this.min.y = Math.min(this.min.y, point.y);
        this.max.x = Math.max(this.max.x, point.x);
        this.max.y = Math.max(this.max.y, point.y);
    }

    get size(): Vector2 {
        return this.max.subed(this.min)
    }
    get center() {
        return new Vector2((this.min.x + this.max.x) / 2, (this.min.y + this.max.y) / 2);
    }
}